# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"

default_platform(:ios)

platform :ios do

  lane :tests do |options|
    xcargs = ""
    xcargs << "RELAY_HOST='#{options[:relay_host]}' " if options[:relay_host]
    xcargs << "PROJECT_ID='#{options[:project_id]}' " if options[:project_id]

    xctestrun = ""
    Dir.chdir("..") do
      xctestrun = Dir.glob("**/*#{options[:testplan]}*.xctestrun").first

      puts "Using following xctestrun file:"
      puts xctestrun
    end
  
    run_tests(
      project: 'Example/ExampleApp.xcodeproj',
      scheme: options[:scheme],
      testplan: defined?(xctestrun) ? nil : options[:testplan],
      cloned_source_packages_path: 'SourcePackagesCache',
      destination: 'platform=iOS Simulator,name=iPhone 14',
      derived_data_path: 'DerivedDataCache',
      skip_package_dependencies_resolution: true,
      skip_build: true,
      xcargs: xcargs,
      xcodebuild_formatter: "xcbeautify --preserve-unbeautified",
      output_directory: "test_results",
      result_bundle: true,
      buildlog_path: "test_results",
      output_types: "junit",
      xctestrun: xctestrun,
      test_without_building: true,
    )
  end

  lane :build do |options|
    xcodebuild(
      project: 'Example/ExampleApp.xcodeproj',
      scheme: options[:scheme],
      destination: 'platform=iOS Simulator,name=iPhone 14',
      xcargs: "-clonedSourcePackagesDirPath SourcePackagesCache -derivedDataPath DerivedDataCache"
    )
  end

  lane :resolve do |options|
    xcodebuild(
      project: 'Example/ExampleApp.xcodeproj',
      scheme: options[:scheme],
      destination: 'platform=iOS Simulator,name=iPhone 13',
      xcargs: "-resolvePackageDependencies -clonedSourcePackagesDirPath SourcePackagesCache -derivedDataPath DerivedDataCache"
    )
  end

  lane :release_testflight do |options|
    setup_ci
    api_key = app_store_connect_api_key(
      key_id: ENV["APPLE_KEY_ID"],
      issuer_id: ENV["APPLE_ISSUER_ID"],
      key_content: ENV["APPLE_KEY_CONTENT"],            
      duration: 1200,            
      in_house: false,
    )
    match(
      readonly: false,
      type: "appstore",
      app_identifier: ENV["MATCH_IDENTIFIERS"],
      git_url: "https://github.com/WalletConnect/match-swift.git",
      git_basic_authorization: options[:token],
      api_key: api_key,
      include_all_certificates: true,
      force_for_new_devices: true,
      force_for_new_certificates: true
    )
    number = latest_testflight_build_number(
      app_identifier: ENV["APP_IDENTIFIER"],
      username: options[:username],
    )
    increment_build_number(
      build_number: number + 1,
      xcodeproj: "Example/ExampleApp.xcodeproj"
    )
    gym(
      configuration: "Release",
      project: "Example/ExampleApp.xcodeproj",
      scheme: ENV["SCHEME"],
      export_method: "app-store",
      xcargs: "RELAY_HOST='#{options[:relay_host]}' PROJECT_ID='#{options[:project_id]}'"
    )
    upload_to_testflight(
      apple_id: ENV["APPLE_ID"],
      app_identifier: ENV["APP_IDENTIFIER"],
      skip_waiting_for_build_processing: true,
      distribute_external: true,
      changelog: "#{ENV["SCHEME"]} app weekly build 🚀",
      notify_external_testers: false,
    )
    clean_build_artifacts()
  end


end